package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtFormDetails;
import com.ruoyi.patrol.domain.vo.PtFormDetailVo;
import com.ruoyi.patrol.mapper.PtFormDetailsMapper;
import com.ruoyi.patrol.service.IPtFormDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 单详情Service业务层处理
 *
 * @author lkm
 * @date 2022-11-12
 */
@Service
public class PtFormDetailsServiceImpl implements IPtFormDetailsService {
    @Autowired
    private PtFormDetailsMapper ptFormDetailsMapper;

    /**
     * 查询单详情
     *
     * @param id 单详情ID
     * @return 单详情
     */
    @Override
    public PtFormDetails selectPtFormDetailsById(Long id) {
        return ptFormDetailsMapper.selectPtFormDetailsById(id);
    }

    /**
     * 查询单详情列表
     *
     * @param ptFormDetails 单详情
     * @return 单详情
     */
    @Override
    public List<PtFormDetails> selectPtFormDetailsList(PtFormDetails ptFormDetails) {
        return ptFormDetailsMapper.selectPtFormDetailsList(ptFormDetails);
    }

    /**
     * 新增单详情
     *
     * @param ptFormFillings 问题集合
     * @param formid         表单id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PtFormDetails> insertPtFormDetails(List<PtFormDetailVo.Formlist> ptFormFillings, Long formid) {
        ptFormFillings.remove(ptFormFillings.size()-1);
        List<PtFormDetails> ptFormDetails = new ArrayList<>();
        int number = 1;
        String username = SecurityUtils.getUsername();
        ptFormFillings.forEach((v) -> {
            PtFormDetails formDetails = new PtFormDetails();
            formDetails.setFormId(formid);
            formDetails.setTitle(v.getTitle());
            formDetails.setType((v.getType() == 1) ? 0 : 1);
            formDetails.setCategory(v.getCategory());
            formDetails.setSerialno((long) number);
            formDetails.setCreateTime(DateUtils.getNowDate());
            formDetails.setCreateBy(username);
            ptFormDetails.add(formDetails);
        });
        System.out.println("题目数量-------"+ptFormFillings.size());
        ptFormDetailsMapper.insertPtFormDetails(ptFormDetails);
        return ptFormDetails;
    }

    /**
     * 修改单详情
     *
     * @param ptFormDetails 单详情
     * @return 结果
     */
    @Override
    public int updatePtFormDetails(PtFormDetails ptFormDetails) {
        ptFormDetails.setUpdateTime(DateUtils.getNowDate());
        return ptFormDetailsMapper.updatePtFormDetails(ptFormDetails);
    }

    /**
     * 批量删除单详情
     *
     * @param ids 需要删除的单详情ID
     * @return 结果
     */
    @Override
    public int deletePtFormDetailsByIds(Long[] ids) {
        return ptFormDetailsMapper.deletePtFormDetailsByIds(ids);
    }

    /**
     * 删除单详情信息
     *
     * @param id 单详情ID
     * @return 结果
     */
    @Override
    public int deletePtFormDetailsById(Long id) {
        return ptFormDetailsMapper.deletePtFormDetailsById(id);
    }
}
